#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int a[N], b[N], ans;
bool vis[N];
int t;
signed main()
{
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 1;i <= n;i++)cin >> a[i];
        for (int i = 1;i <= n;i++)cin >> b[i], vis[i] = 0;
        if (a[n] == b[n]) { cout << n << "\n";continue; }
        ans = 0;
        for (int i = n - 1;i >= 1;i--) {
            if (a[i] == b[i] || a[i] == a[i + 1] || b[i] == b[i + 1] || vis[a[i]] || vis[b[i]]) {
                ans = i;
                break;
            }
            vis[a[i + 1]] = vis[b[i + 1]] = 1;
        }
        cout << ans << "\n";
    }
    return 0;
}